package com.woniu.core.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.core.entity.CoreEnterprise;
import com.woniu.core.entity.vo.CoreEnterpriseVO;
import com.woniu.core.mapper.CoreEnterpriseMapper;
import com.woniu.core.service.CoreEnterpriseService;
import com.woniu.util.utils.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CoreEnterpriseServiceImpl implements CoreEnterpriseService {
    @Autowired
    private CoreEnterpriseMapper coreEnterpriseMapper;

    /**
     * 新增核心企业信息
     * @param coreEnterprise  核心企业信息
     * @return
     */
    @Override
    public HttpResult<?> insertCore(CoreEnterprise coreEnterprise) {


        try {
            coreEnterpriseMapper.insert(coreEnterprise);
            return new HttpResult<>().ok();
        } catch (Exception e) {
//           e.printStackTrace();
            return new HttpResult<>().fail();
        }


    }

    /**
     * 查找核心企业
     * @param coreEnterpriseVO 需要查找的信息
     * @return
     */
    @Override
    public HttpResult<?> selectCore(CoreEnterpriseVO coreEnterpriseVO) {
        try {
            List<CoreEnterprise> coreEnterprises = coreEnterpriseMapper.selectCore(coreEnterpriseVO);
            return new HttpResult<>().ok(coreEnterprises);
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    /**
     * 修改核心企业
     * @param coreEnterprise 核心企业实体
     * @return
     */
    @Override
    public HttpResult<?> updateCore(CoreEnterprise coreEnterprise) {
        int result = coreEnterpriseMapper.updateById(coreEnterprise);
        if(result > 0){
          return new HttpResult<>().ok();
        }
        return new HttpResult<>().fail();
    }

    /**
     * 删除核心企业
     * @param coreId 核心企业ID
     * @return
     */
    @Override
    public HttpResult<?> deleteCore(Integer coreId) {
        int result = coreEnterpriseMapper.deleteById(coreId);
        if(result > 0){
            return new HttpResult<>().ok();
        }
        return new HttpResult<>().fail();
    }

    /**
     * 通过核心企业名称查询
     * @param enterpriseName
     * @return
     * @Author:杨顺
     */
    @Override
    public HttpResult queryCoreByName(String enterpriseName) {
        QueryWrapper<CoreEnterprise> wrapper = new QueryWrapper<CoreEnterprise>();
        wrapper.notLike("enterprise_name", enterpriseName);
        List<CoreEnterprise> list = coreEnterpriseMapper.selectList(wrapper);
        return new HttpResult<>().ok(list);
    }

    @Override
    public HttpResult<?> findCoreByName() {
        try {
            List<CoreEnterprise> core = coreEnterpriseMapper.findCoreByName();
            return new HttpResult().ok(core);
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult().fail();
        }
    }


    /**
     * 通过id查找核心企业
     * @param coreId   id
     * @return
     */
    public HttpResult<?> queryCoreById(Integer coreId){
        CoreEnterprise coreEnterprise = coreEnterpriseMapper.selectById(coreId);
        if(coreEnterprise != null){
            return new HttpResult<>().ok(coreEnterprise);
        }
        return new HttpResult<>().fail();
    }


}
